#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

const std::string web_path = "./wwwroot";
const std::string default_path = "index.html";
const std::string not_found = "./wwwroot/404.html";

#define LINE "\r\n"
#define LINESIZE strlen(LINE)

namespace httptool
{

    class Request
    {
    public:
        Request()
        {
        }

        // get url _httpversion\r\n
        bool getandsubstrline()
        {
            auto pos = _reqBuffer.find(LINE);
            if (pos == std::string::npos)
                return false;
            std::string line = _reqBuffer.substr(0, pos);
            _reqBuffer.erase(0, pos);

            std::stringstream ss(line);
            ss >> _means >> _url >> _httpversion;
            // 使用 sstream流进行输入
            return true;
        }

    public:
        std::string _means;

        std::string _url;
        std::string _path; // 现在我们根据路径来搞事情
        std::string _httpversion;

        std::string _statusline;
        std::string _readHeader;
        std::string _readgas;
        std::string _readsize;

        std::string _reqBuffer;
    };

    class Responce
    {
    public:
        Responce()
        {
        }

        bool getBoby(const std::string path, const int buffersize)
        {
            // 根据路径来返回获取的结果
            std::ifstream file;

            file.open(path);
            
          
         
            if (file.is_open())
            {
                char buffer[buffersize];
                file.read(buffer, buffersize);
                _boby = buffer;
            }
            else
            {
                return false;
            }

            file.close();

            return true;
        }

        bool getsize(const std::string path)
        {
            //如果是正确的路径我们就正常的打开，要是不是正确的路径我们直接打开 "./wwwroot/404.html"
            struct stat statmessage;
            int n = stat(path.c_str(), &statmessage);
            if (n == -1)
                return false;

            _size = statmessage.st_size;
            return true;
        }

        bool getheader(const std::string path)
        {
             
             auto pos = path.rfind('.');
             if(pos == std::string::npos) return false;

             std::string header = "Context-Type: ";
             if(path.substr(pos) == ".html")
             header += "text/html";
             else if(path.substr(pos) == ".jpg")
             header += "image/jpeg";
             
             _respheader = header;
             return true;
        }
    public:
        // 要返回回去的，size的大小，使用stat接口
        int _size;

        std::string _respheader;
        std::string _boby;
        std::string _respBuffer;
    };

    void responceMessage(Request &req, Responce &resp)
    {
        std::string send = "";
        std::string start = "-------------------------------------------hello  world------------------------------------------\r\n";
        std::string end = "-------------------------------------------beybye world------------------------------------------\r\n";

        // 获取对方要访问的路径
        std::string path = "";
        path += web_path;
        if (req._url == "/")
            req._path = path + req._url + default_path;
        else
            req._path = path += req._url;

        // if(req._path == "/rseach")
        // {
        //    // CPP任务  
        // }
        // if(req._path == "/text.python")
        // {
        //     //创建子进程，管道，
        //     //去执行子任务等
        // }

        start += req._means;
        start += LINE;
        start += req._url;
        start += LINE;
        start += req._httpversion;
        start += LINE;
        start += req._path;
        start += LINE;
        start += req._reqBuffer;
        start += end;

        std::cout << start << std::endl;

        //如果没找到就直接去找 path
        if (!resp.getsize(req._path))
        {
            resp.getsize("./wwwroot/404.html");
            req._path = not_found;
        }

        std::string respBlank = "http/1.1 307 Moved Permanently";
        // 告诉web网页返回的是 html的容器
        if(!resp.getheader(req._path)) 
        {
            std::cout << "请求信息的路径有误" << std::endl;
        }

        std::string respgas = LINE;


        std::string redirct = "Location: https://www.qq.com/";

        std::string resplength = "Context-Length: ";
        resplength += std::to_string(resp._size);

        if (!resp.getBoby(req._path, resp._size + 1)) 
        {
            resp.getBoby("./wwwroot/404.html", resp._size + 1);
        }
        std::string boby = resp._boby;

        

        // send += start;
        send += respBlank;
        send += LINE;
        send += resplength;
        send += LINE;
       // send += redirct;
       // send += LINE;
        send += resp._respheader;
        send += LINE;
        send += respgas;
       
        send += boby;
        // send += end;
        std::cout <<  send << std::endl;

        resp._respBuffer = send;
    }
}
